package com.ys.peony.utils;

import android.text.TextUtils;

import com.ys.peony.app.YSConstant;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * describe:文件流工具类
 * author:yukang
 * date:2017/09/12
 */
public class FileUtils {
    /**
     * describe:File转Byte数组
     * author:zhangcong
     * date:2017/6/1 22:28
     */
    public static byte[] File2byte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * describe:Byte数组转文件
     * author:zhangcong
     * date:2017/6/1 22:29
     */
    public static void byte2File(byte[] buf, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath:absolute file path
     */
    public static Boolean doDeleteFile(String filePath) {
        File file = new File(filePath);
        if (file == null || !file.exists()) {
            return true;
        }
        if (file.isDirectory()) {
            String[] children = file.list();
            if (children != null && children.length > 0) {
                for (String path : children) {
                    //递归删除文件
                    boolean success = doDeleteFile(path);
                    if (!success) {
                        return false;
                    }
                }
            }
        }
        return file.delete();
    }

    /**
     * 删除文件
     * @param dir
     */
    public static boolean doDeleteDir(File dir){
        if(dir == null) {
            return false;
        }
        if(dir.isDirectory()){
            String[] children=dir.list();
            if(children!=null && children.length>0){
                for(String path:children){
                    //递归删除文件
                    boolean success=doDeleteDir(new File(dir,path));
                    if(!success){
                        return false;
                    }
                }
            }
        }
        return dir.delete();
    }

    /**
     * 描述：获取本地Json数据
     *
     * @param path 文件绝对路径
     */
    public static String file2Json(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        String tempstr = "";
        try {
            File file = new File(path);
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                while ((tempstr = br.readLine()) != null)
                    sb.append(tempstr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 描述：json数据存入本地文件
     *
     * @param path 文件绝对路径
     * @param data json字符串
     */
    public static void json2File(String path, String data) {
        FileOutputStream out = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }
            out = new FileOutputStream(file, false);
            out.write(data.toString().getBytes("utf-8"));//注意需要转换对应的字符集
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
